import React, { useImperativeHandle, forwardRef, useRef } from 'react'
import { Form, Input, Select, Row, Col, DatePicker, Switch, Upload, Button } from 'antd'
import { UploadOutlined } from '@ant-design/icons'
import SubjectSelector from './SubjectSelector'
import AddressInput from './AddressInput'
import style from '../Modal.module.less'
import { getUploadPropsFn } from '@js/tool'

const { Item } = Form
const { Option } = Select
const { TextArea } = Input
const shortLabel = { span: 6 }
const shortWrap = { span: 16 }

// 改变仲裁主体回调
function onChangeContractId (o, formRef, addressRef, pState, pDispatch) {
  const currentAddressList = pState.addressList.filter(li => li.contractid === o.id && li.status === 1).map((li, i) => {
    li.value = `${li.address}${i}`
    li.label = `(${i + 1})、${li.address || '无地址'}`
    return li
  })
  pDispatch({ type: 'currentAddressList', data: currentAddressList })
  if (currentAddressList.length === 1) {
    addressRef.current.changeAddress(formRef, currentAddressList[0], pState)
  }
}

// 设置送达人员
function setSender (formRef, option) {
  formRef.current.setFieldsValue({
    sender: option.value,
    senderid: option.key
  })
}

// 设置公证人员
function setNotary (formRef, option) {
  formRef.current.setFieldsValue({
    notary: option.value,
    notaryid: option.key
  })
}

// formRef, caseDetail, data, theme, addressList, contractList, setContractList, currentAddressList, setCurrentAddressList
function Main ({ formRef, pState, pDispatch }, ref) {
  const subjectRef = useRef(null)
  const addressRef = useRef(null)
  useImperativeHandle(ref, () => ({
    changeSubject: (type, formRef, caseDetail, data, dispatch) => {
      subjectRef.current.changeSubject(type, formRef, caseDetail, data, dispatch)
    }
  }))
  return (
    <>
      <Item name='status' noStyle><span /></Item>
      <Item name='contract_type' noStyle><span /></Item>
      <Item name='id' noStyle><span /></Item>
      <SubjectSelector ref={subjectRef} formRef={formRef} pState={pState} pDispatch={pDispatch} onChangeContractId={o => onChangeContractId(o, formRef, addressRef, pState, pDispatch)} />
      <AddressInput ref={addressRef} addressList={pState.currentAddressList} pState={pState} formRef={formRef} />
      <Item name='contract' label='收件人'>
        <Input placeholder='请输入' />
      </Item>
      <Item name='structure' label='单位'>
        <Input placeholder='请输入单位' />
      </Item>
      <Item name='senderid' noStyle><span /></Item>
      <Item name='sender' label='送达人员'>
        <Select showSearch placeholder='请选择送达人员' onChange={(v, option) => setSender(formRef, option)}>
          {pState.staffs.map((li, i) => <Option key={li.id} value={li.name}>{li.name}</Option>)}
        </Select>
      </Item>
      <Item name='notaryid' noStyle><span /></Item>
      <Item name='notary' label='公证人' rules={[{ required: true, message: '请选择公证人' }]}>
        <Select showSearch placeholder='请选择公证人' onChange={(v, option) => setNotary(formRef, option)}>
          {pState.notaries.map(li => <Option key={li.id} value={li.name}>{li.name}</Option>)}
        </Select>
      </Item>
      <Row>
        <Col span={12}>
          <Item name='arrive_datetime' label='送达日期' labelCol={shortLabel} wrapperCol={shortWrap} rules={[{ type: 'object', required: true, message: '请选择送达日期' }]}>
            <DatePicker placeholder='请选择送达日期' style={{ width: '100%' }} />
          </Item>
        </Col>
      </Row>
      <Item name='file' label='送达情况材料'>
        <Upload {...getUploadPropsFn.call(this, formRef, '.jpg,.jpeg,.png', pState.fileList, v => pDispatch({ type: 'fileList', data: v }))}>
          <Button>
            <UploadOutlined /> 上传文件
          </Button>
          <span className={style['upload-placeholder']}>支持扩展名：.png .jpg</span>
        </Upload>
      </Item>
      <Item name='content' label='送达内容' rules={[{ required: true, message: '请输入送达内容' }]}>
        <TextArea rows={3} placeholder='请输入送达内容' />
      </Item>
      <Item name='remark' label='备注'>
        <TextArea rows={3} placeholder='请输入备注' />
      </Item>
      <Item name='islast_known_place' label='是否为最后一个为人所知的地址' labelCol={{}} valuePropName='checked'>
        <Switch checkedChildren='是' unCheckedChildren='否' />
      </Item>
    </>
  )
}

export default forwardRef(Main)
